/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.zeos.bean;

import br.zeos.ZLanguage;
import br.zeos.ZDataSource;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.security.auth.callback.LanguageCallback;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author virtual
 */
public class ZDbNavigator extends javax.swing.JPanel {

    ZDataSource zDataSource = null;

    public void setEnabledFirst(boolean value) {
        this.primeiro.setEnabled(value);
    }

    public void setEnabledPrevious(boolean value) {
        this.anterior.setEnabled(value);
    }

    public void setEnabledNext(boolean value) {
        this.proximo.setEnabled(value);
    }

    public void setEnabledLast(boolean value) {
        this.ultimo.setEnabled(value);
    }

    public void setEnabledAdd(boolean value) {
        this.adicionar.setEnabled(value);
    }

    public void setEnabledApply(boolean value) {
        this.aplicar.setEnabled(value);
    }

    public void setEnabledRemove(boolean value) {
        this.remover.setEnabled(value);
    }

    public void setEnabledOK(boolean value) {
        this.ok.setEnabled(value);
    }

    public ZDataSource getDataSource() {
        return zDataSource;
    }

    public void setDataSource(ZDataSource DataSource) {
        ZLanguage language = new ZLanguage();
        primeiro.setToolTipText(language.firstButton);
        anterior.setToolTipText(language.previousButton);
        proximo.setToolTipText(language.nextButton);
        ultimo.setToolTipText(language.lastButton);
        adicionar.setToolTipText(language.addButton);
        aplicar.setToolTipText(language.applyButton);
        ok.setToolTipText(language.okButton);
        remover.setToolTipText(language.removeButton);
        
        primeiro.setEnabled(true);
        anterior.setEnabled(true);
        proximo.setEnabled(true);
        ultimo.setEnabled(true);
        adicionar.setEnabled(true);
        aplicar.setEnabled(true);
//        jButton7.setEnabled(true);
        ok.setEnabled(true);
        remover.setEnabled(true);
        this.zDataSource = DataSource;
        this.zDataSource.moveFirst();
        this.zDataSource.addUpdateListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                int index = zDataSource.getIndex();
                int max = zDataSource.getData().size();
                UpdateForm(index, max);
            }
        });
    }

    public void UpdateForm(int index, int max) {
        

    }

    /**
     * Creates new form ZDbNavigator
     */
    public ZDbNavigator(ZDataSource origem) {
        this.zDataSource = origem;
        initComponents();
        this.zDataSource.addUpdateListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                int index = zDataSource.getIndex();
                int max = zDataSource.getData().size();
                UpdateForm(index, max);
            }
        });
    }

    public ZDbNavigator() {
        initComponents();
        primeiro.setEnabled(false);
        anterior.setEnabled(false);
        proximo.setEnabled(false);
        ultimo.setEnabled(false);
        adicionar.setEnabled(false);
        aplicar.setEnabled(false);
        ok.setEnabled(false);
        remover.setEnabled(false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        primeiro = new javax.swing.JButton();
        anterior = new javax.swing.JButton();
        proximo = new javax.swing.JButton();
        ultimo = new javax.swing.JButton();
        adicionar = new javax.swing.JButton();
        aplicar = new javax.swing.JButton();
        ok = new javax.swing.JButton();
        remover = new javax.swing.JButton();
        filtro = new javax.swing.JTextField();

        primeiro.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/zeos/images/001.png"))); // NOI18N
        primeiro.setToolTipText("");
        primeiro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                primeiroActionPerformed(evt);
            }
        });

        anterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/zeos/images/002.png"))); // NOI18N
        anterior.setToolTipText("");
        anterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                anteriorActionPerformed(evt);
            }
        });

        proximo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/zeos/images/003.png"))); // NOI18N
        proximo.setToolTipText("");
        proximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                proximoActionPerformed(evt);
            }
        });

        ultimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/zeos/images/004.png"))); // NOI18N
        ultimo.setToolTipText("");
        ultimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ultimoActionPerformed(evt);
            }
        });

        adicionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/zeos/images/005.png"))); // NOI18N
        adicionar.setToolTipText("");
        adicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adicionarActionPerformed(evt);
            }
        });

        aplicar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/zeos/images/006.png"))); // NOI18N
        aplicar.setToolTipText("");
        aplicar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aplicarActionPerformed(evt);
            }
        });

        ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/zeos/images/008.png"))); // NOI18N
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        remover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/zeos/images/007.png"))); // NOI18N
        remover.setToolTipText("");
        remover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removerActionPerformed(evt);
            }
        });

        filtro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filtroActionPerformed(evt);
            }
        });
        filtro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                filtroKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(primeiro)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(anterior)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(proximo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ultimo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(adicionar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(aplicar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(remover)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(filtro, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ok)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(primeiro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(anterior, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(proximo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(ultimo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(adicionar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(aplicar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(remover, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(ok, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(filtro)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void primeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_primeiroActionPerformed
        zDataSource.moveFirst();
    }//GEN-LAST:event_primeiroActionPerformed

    private void anteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_anteriorActionPerformed
        zDataSource.movePrevious();
    }//GEN-LAST:event_anteriorActionPerformed

    private void proximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_proximoActionPerformed
        zDataSource.moveNext();
    }//GEN-LAST:event_proximoActionPerformed

    private void ultimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ultimoActionPerformed
        zDataSource.moveLast();
    }//GEN-LAST:event_ultimoActionPerformed

    private void adicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionarActionPerformed
        zDataSource.Insert();
    }//GEN-LAST:event_adicionarActionPerformed

    private void aplicarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aplicarActionPerformed
        zDataSource.Update();
    }//GEN-LAST:event_aplicarActionPerformed

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        this.zDataSource.Filter(this.filtro.getText());
        zDataSource.moveFirst();
    }//GEN-LAST:event_okActionPerformed

    private void removerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removerActionPerformed
        ZLanguage language = new ZLanguage();
        int confirm = JOptionPane.showConfirmDialog(null, language.deleteMensage, null, JOptionPane.YES_NO_OPTION);
        if ( confirm == 0 ){
            zDataSource.Delete();
            zDataSource.moveLast();
        }
    }//GEN-LAST:event_removerActionPerformed

    private void filtroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filtroActionPerformed

    }//GEN-LAST:event_filtroActionPerformed

    private void filtroKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_filtroKeyReleased
        if ( evt.getKeyCode() == 10 ){
            this.zDataSource.Filter(this.filtro.getText());
            zDataSource.moveFirst();
        }else if ( evt.getKeyCode() == 8 ){
                if ( this.filtro.getText().isEmpty() ){
                    this.zDataSource.Filter(this.filtro.getText());
                    zDataSource.moveFirst();
                }
        }
        
    }//GEN-LAST:event_filtroKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton adicionar;
    private javax.swing.JButton anterior;
    private javax.swing.JButton aplicar;
    private javax.swing.JTextField filtro;
    private javax.swing.JButton ok;
    private javax.swing.JButton primeiro;
    private javax.swing.JButton proximo;
    private javax.swing.JButton remover;
    private javax.swing.JButton ultimo;
    // End of variables declaration//GEN-END:variables
}
